<template>
  <div class="goods">
    <div class="warp">
      <Crumb> </Crumb>
      <img src="../assets/img/banner.4c6b6225.png" width="100%" alt="" />
      <ul class="options">
        <li>
          <strong>排序：</strong>
          <span
            :class="{ active: options1Index == index }"
            v-for="(item, index) in options1"
            :key="index"
            @click="options1Fn(index, item.min, item.max)"
            >{{ item.txt }}</span
          >
        </li>
        <li>
          <strong>分类：</strong>
          <span
            :class="{ active: options2Index == index }"
            v-for="(item, index) in options2"
            :key="index"
            @click="options2Fn(index, item.type)"
            >{{ item.txt }}</span
          >
        </li>
      </ul>
      <List :arr="goodsListShow" />
      <div v-show="!isReachBottom" style="display: flex">
        <div class="loading"><i class="iconfont icon-loading"></i></div>
        loading...
      </div>
      <div v-show="isReachBottom">加载完毕</div>
    </div>
  </div>
</template>                            
<script>
import { GoodsSearchAPI } from "@/request/api";
import Crumb from "../components/Crumb.vue";
import List from "../components/home/List.vue";
import { getScrollTop, getClientHeight, getScrollHeight } from "@/utils";
export default {
  data() {
    return {
      // 栏目 目前可提供的参数是 1->精品推荐 2-> 热门兑换 0->全部
      did: 0,
      // ( 1->实体商品 2->虚拟商品 0->全部 )
      type: 0,
      // 大于多少积分，最少是0
      min: 0,
      // 少于多少积分，最多是10000，当传入0的时候，会直接返回所有商品，无视后台逻辑。
      max: 0,
      // 商品关键词
      keyword: "",
      // 商品列表数组
      goodsList: [],
      //优化后的数组
      goodsListShow: [],
      //排序数组
      options1: [
        { min: 0, max: 0, txt: "全部" },
        // "我还可以兑换"的max值是Header组件的coin值
        { min: 0, max: 0, txt: "我还可以兑换" },
        { min: 0, max: 500, txt: "0-500分" },
        { min: 500, max: 1000, txt: "500-1000分" },
        { min: 1000, max: 1500, txt: "1000-1500分" },
        { min: 1500, max: 2500, txt: "1500-2500分" },
        { min: 2500, max: 6500, txt: "2500-6500分" },
        { min: 6500, max: 10000, txt: "6500-10000分" },
      ],
      //排序索引
      options1Index: 0,
      // 分类数组项
      options2: [
        { type: 0, txt: "全部" },
        { type: 1, txt: "实体商品" },
        { type: 2, txt: "虚拟商品" },
      ],
      //分类索引
      options2Index: 0,
      //当前页码
      page: 1,
      //每页条数
      size: 8,
      //是否在加载
      isLoading: false,
      //是否到达底部
      isReachBottom: false,
    };
  },
  components: {
    Crumb,
    List,
  },
  methods: {
    async goodsSearch() {
      let res = await GoodsSearchAPI({
        did: this.did,
        type: this.type,
        min: this.min,
        max: this.max,
        keyword: this.keyword,
      });
      if (!res) return;
      this.goodsList = res.data;
      //从goodsList中获取8条数据到goodsListShow(filter 筛选8条数据)
      this.goodsListShow = this.goodsList.filter(
        (item, index) => index < this.size
      );
      //第一次进入都会设置page为1默认显示没加载完
      this.page = 1;
      this.isReachBottom = false;
      //判断是否加载完成,如果完成设置为true
      if (this.goodsListShow.length == this.goodsList.length) {
        //加载完毕了
        this.isReachBottom = true;
        return;
      }
    },
    //点击高亮
    options1Fn(idx, min, max) {
      this.options1Index = idx;
      this.min = min;
      this.max = max;
      this.goodsSearch();
    },
    options2Fn(idx, type) {
      //高亮
      this.options2Index = idx;
      //查询条件
      this.type = type;
      //查询方法
      this.goodsSearch();
    },
    //滚动时执行
    scrollFn() {
      //判断是否到达需要加载数据的位置
      if (getClientHeight() + getScrollTop() > getScrollHeight() - 240) {
        //判断数据是否已经全部加载
        if (this.goodsListShow.length == this.goodsList.length) {
          //加载完毕了就不在继续执行
          this.isReachBottom = true;
          return;
        }
        //节流判断,如果已经开始加载就不继续执行
        if (this.isLoading == false) {
          this.isLoading = true;
          this.page++;
          setTimeout(() => {
            for (
              let i = (this.page - 1) * this.size;
              i < this.page * this.size;
              i++
            ) {
              this.goodsList[i] && this.goodsListShow.push(this.goodsList[i]);
            }
            this.isLoading = false; //加载完毕 === 没有再加载数据
          }, 2000);
        }
      }
    },
  },
  created() {
    let keyword = this.$route.query.keyword || "";
    this.keyword = keyword;
    this.goodsSearch();
  },
  //挂载完成后
  mounted() {
    //监听滚动事件
    window.addEventListener("scroll", this.scrollFn);
  },
  //销毁之前
  beforeDestroy() {
    //取消监听
    window.removeEventListener("scroll", this.scrollFn);
  },
  watch: {
    "$route.query.keyword": {
      handler(newVal, oldval) {
        this.keyword = newVal;
        this.goodsSearch();
      },
    },
  },
};
</script>
 
<style lang = "less" scoped>
@import "../assets/css/public.less";

.goods {
  padding-bottom: 50px;
  background: #efefef;
  padding-top: 20px;
  .options {
    padding-top: 20px;
    padding-bottom: 40px;
    li {
      margin-top: 20px;
      strong {
        color: #000;
        font-weight: bold;
      }
      span {
        margin-right: 20px;
        margin-left: 10px;
        cursor: pointer;
        color: #999;
        &.active {
          color: @base-color;
          font-weight: bold;
        }
      }
    }
  }
}
</style>